package com.cloudfun.campusshare.schedule;

import com.cloudfun.campusshare.common.constant.BidStatus;
import com.cloudfun.campusshare.entity.BidEntity;
import com.cloudfun.campusshare.repo.BidEntityRepo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by Huqin on 2020/4/8
 */
@Slf4j
@Component
public class BidStatusChangeSchedule extends BaseScheduleTask {

    @Autowired
    private BidEntityRepo bidEntityRepo;

    /**
     * 每天凌晨0点执行
     */
    @Scheduled(cron = "1 0 0 * * ?")
    public void changeBidStatus() {
        this.execute();
    }

    @Override
    protected String getTaskName() {
        return "招标信息状态更新";
    }

    @Override
    protected void doExecute() {
        Date now = new Date();
        List<BidEntity> bidEntities = bidEntityRepo.findAllByStatus(BidStatus.UNFINISHED);
        for (BidEntity bidEntity : bidEntities) {
            Date endDate = new Date(Long.parseLong(bidEntity.getEndTime()));
            if (endDate.before(now)) {
                bidEntity.setStatus(BidStatus.EXPIRED);
            }
        }
        bidEntityRepo.saveAll(bidEntities);
    }

}
